#include "music.h"
#include "ui_music.h"
#include "music_search.h"
#include <QIcon>
#include <QPalette>
music::music(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::music)
{
    ui->setupUi(this);


    //设置背景为白色
    this->setStyleSheet("background-color: rgb(255, 255, 255)");
    //定义另一个类的对象
    Music_Search = new music_search(ui->frame,this);
    Music_Search->hide();//将这个画面隐藏
    //搜索
     ui->search->setStyleSheet("QPushButton{font: 25 14pt '微软雅黑 Light';color: rgb(255,255,255);background-color: rgb(20,196,188);"
                               "border: none;border-radius:15px;}"
                               "QPushButton:hover{background-color: rgb(22,218,208);}"//hover
                               "QPushButton:pressed{background-color: rgb(17,171,164);}");//pressed
    //搜索框
     ui->lineEdit->setStyleSheet("font: 20 12pt '微软雅黑 Light';"
                                 "color: rgb(31,31,31);"
                                 "background:transparent;border-width:0;border-style:solid;"//给lineEdit隐藏
                                 "border:2px solid rgb(20,196,188);"//边框宽度为2 solid表示为实线
                                 );

     //最小化
     QPixmap pixmap1(":/photo/min_green.svg");
     ui->MIN->setPixmap(pixmap1);
     ui->MIN->setScaledContents(true);
     //关闭
     QPixmap pixmap2(":/photo/close_green.svg");
     ui->CLOSE->setPixmap(pixmap2);
     ui->CLOSE->setScaledContents(true);
     //图标
     QPixmap pixmap3(":/photo/label_green.svg");
     ui->label_3->setPixmap(pixmap3);
     ui->label_3->setScaledContents(true);



     QPalette pl;
     pl.setBrush(QPalette::Base, QBrush(QColor(255,0,0,0))); // 设置背景为完全透明
     ui->frame->setStyleSheet("background:transparent; border-width:0; border-style:outset;");
     ui->frame->setPalette(pl);
     setWindowFlag(Qt::FramelessWindowHint,true);//去掉主窗口的边框
//     setAttribute(Qt::WA_TranslucentBackground,true);//把主窗口透明

    //设置水平滑块的样式
    ui->horizontalSlider->setStyleSheet("QSlider::handle:horizontal { background: rgb(20,196,188); }"
                                        "QSlider::groove:horizontal {"
                                                                 "    border: 1px solid rgb(20,196,188);"
                                                                 "    height: 10px;"
                                                                 "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #F3EDFF, stop:1 #F3EDFF);"
                                                                 "}"
                                        "QSlider::handle:horizontal {"
                                                                 "    background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 rgb(20,196,188), stop:1 rgb(20,196,188));"
                                                                 "    border: 1px solid #5c5c5c;"
                                                                 "    width: 18px;"
                                                                 "}"
//                                                                 "QSlider::add-page:horizontal {"
//                                                                 "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #505050, stop:1 #636363);"
//                                                                 "}"
                                                                 "QSlider::sub-page:horizontal {"
                                                                 "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(20,196,188), stop:1 rgb(20,196,188));" // 已滑过区域颜色
                                                                 "}");
    //
    ui->verticalSlider->hide();
    ui->label_4->hide();
    //设置声音滑块范围
    static_voice = 0;
    ui->verticalSlider->setRange(0,100);
    ui->verticalSlider->setValue(100);
    ui->verticalSlider->setStyleSheet("QSlider::handle:vertical { background: rgb(20,196,188); }");
    voice_mun = 100;
    //播放按钮图标
    QString imgPath = ":/photo/play_green.svg";
    QString stylesheet = QString("QPushButton{border-image: url(%1);}").arg(imgPath);
    ui->pushButton->setStyleSheet(stylesheet);
    //下一首按钮图标
    imgPath = ":/photo/last_song_green.svg";
    stylesheet = QString("QPushButton{border-image: url(%1);}").arg(imgPath);
    ui->pushButton_2->setStyleSheet(stylesheet);
    //上一首按钮图标
    QString imgPath1 = ":/photo/pre_song_green.svg";
    stylesheet = QString("QPushButton{border-image: url(%1);}").arg(imgPath1);
    ui->pushButton_4->setStyleSheet(stylesheet);
    //声音图标
    QString imgPath2 = ":/photo/voice_green.svg";
    stylesheet = QString("QPushButton{border-image: url(%1);}").arg(imgPath2);
    ui->pushButton_3->setStyleSheet(stylesheet);
    //关闭按钮
    connect(ui->CLOSE,SIGNAL(clicked111()),this,SLOT(close_clicked()));
    //最小化按钮
    connect(ui->MIN,SIGNAL(clicked111()),this,SLOT(min_clicked()));
}

music::~music()
{
    delete ui;
}


void music::on_search_clicked()
{

    Music_Search->manage->get(QNetworkRequest(QUrl(QString("http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=%0&type=1&offset=0&total=true&limit=10").arg(ui->lineEdit->text()))));
    Music_Search->show();
}

void music::close_clicked()
{
    close();
    qDebug()<<"关闭了窗口"<<endl;
}

void music::min_clicked()
{
    showMinimized();
    qDebug()<<"最小化"<<endl;
}
//设置滑块范围
void music::Sliderall(qint64 time)
{
    qDebug()<<"aaaaaa"<<time<<endl;
    ui->horizontalSlider->setRange(0,time);
    qint64 mm = time%60000;
    qDebug()<<"==========="<<mm/1000<<endl;
    QString songalltime = QString("0%0:%1").arg(int(time/60000)).arg(mm/1000);
    ui->label_2->setText(songalltime);
}
//设置滑块位置
void music::Slidering(qint64 time)
{
    qDebug()<<"===="<<time<<endl;
    ui->horizontalSlider->setValue(time);
    qint64 mm = time%60000;
    qDebug()<<"==========="<<mm/1000<<endl;
    QString songnowtime = QString("0%0:%1").arg(int(time/60000)).arg(mm/1000);
    QString songnowtime1 = QString("0%0:0%1").arg(int(time/60000)).arg(mm/1000);
    if(mm/1000<10)
    {
        ui->label->setText(songnowtime1);
    }
    else
    {
        ui->label->setText(songnowtime);
    }

}
//设置歌名
void music::songename(QString name)
{
    qDebug()<<"mmmmmm"<<name<<endl;
    ui->music_name->setText(name);
}
//暂停和播放
void music::on_pushButton_clicked()
{
    qDebug()<<"qwewqeqw"<<endl;
    if(Music_Search->player->state() == QMediaPlayer::PlayingState)
    {
        Music_Search->player->pause();
        qDebug()<<"1111"<<endl;
        return;
    }
    if(Music_Search->player->state() == QMediaPlayer::PausedState)
    {
        Music_Search->player->play();
        qDebug()<<"2222"<<endl;
        return;
    }
}
//上一首
void music::on_pushButton_4_clicked()
{
    if(Music_Search->song_list_num == 0)
    {
        Music_Search->song_list_num = Music_Search->song_list_num;
        Music_Search->playlist->setCurrentIndex(Music_Search->song_list_num);
        Music_Search->player->setVolume(ui->verticalSlider->value());
        Music_Search->player->play();
        songename(Music_Search->song_name_list.at(Music_Search->song_list_num));
        qDebug()<<"2222"<<endl;
    }
    else
    {
         Music_Search->song_list_num = Music_Search->song_list_num -1;
         Music_Search->playlist->setCurrentIndex(Music_Search->song_list_num);
         Music_Search->player->setVolume(ui->verticalSlider->value());
         Music_Search->player->play();
         songename(Music_Search->song_name_list.at(Music_Search->song_list_num));
         qDebug()<<"333"<<endl;
    }

}
//下一首
void music::on_pushButton_2_clicked()
{
    if(Music_Search->song_list_num == 9)
    {
        Music_Search->song_list_num = Music_Search->song_list_num;
        Music_Search->playlist->setCurrentIndex(Music_Search->song_list_num);
        Music_Search->player->setVolume(ui->verticalSlider->value());
        Music_Search->player->play();
        songename(Music_Search->song_name_list.at(Music_Search->song_list_num));
        qDebug()<<"2222"<<endl;
    }
    else
    {
         Music_Search->song_list_num = Music_Search->song_list_num +1;
         Music_Search->playlist->setCurrentIndex(Music_Search->song_list_num);
         Music_Search->player->setVolume(ui->verticalSlider->value());
         Music_Search->player->play();
         songename(Music_Search->song_name_list.at(Music_Search->song_list_num));
         qDebug()<<"333"<<endl;
    }
}
//移动滑块控制播放位置
void music::on_horizontalSlider_sliderReleased()
{
    Music_Search->player->setPosition(ui->horizontalSlider->value());
}


//声音设置
void music::on_pushButton_3_clicked()
{
//    qDebug()<<11111<<endl;
    if(static_voice == 0)
    {
        ui->verticalSlider->show();
        ui->label_4->show();
        QString aa = QString("%1 %").arg(ui->verticalSlider->value());
        ui->label_4->setText(aa);
        static_voice = 1;
//        qDebug()<<11111<<endl;

        return;
    }
    if(static_voice == 1)
    {
        ui->verticalSlider->hide();
        ui->label_4->hide();
        static_voice = 0;
//        qDebug()<<Music_Search->player->state()<<endl;
        return;
    }
}


//声音滑块设置
void music::on_verticalSlider_sliderReleased()
{
//    PlayingState
//            PausedState

//            StoppedState
//    if()
            qDebug()<<ui->verticalSlider->value()<<endl;
            voice_mun = ui->verticalSlider->value();
            Music_Search->player->setVolume(voice_mun);
            QString aa = QString("%1 %").arg(ui->verticalSlider->value());
            ui->label_4->setText(aa);

}
